package com.example.kaoqinmanager.activity;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioGroup;

import com.example.kaoqinmanager.R;
import com.example.kaoqinmanager.base.BaseBindingActivity;
import com.example.kaoqinmanager.bean.UserTable;
import com.example.kaoqinmanager.databinding.ActivityLoginBinding;
import com.example.kaoqinmanager.utils.MyToast;

import org.litepal.LitePal;

import java.util.List;

public class LoginActivity extends BaseBindingActivity<ActivityLoginBinding> {

    private int userRole = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        actionBar.setTitle("登录");


        getBinding().btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String strAccount = getBinding().edAccount.getText().toString().trim();
                String strPassWord = getBinding().edPassWord.getText().toString().trim();
                if (TextUtils.isEmpty(strAccount)) {
                    MyToast.showToast("账号不能为空");
                    return;
                }
                if (TextUtils.isEmpty(strPassWord)) {
                    MyToast.showToast("密码不能为空");
                    return;
                }

                List<UserTable> userTables = LitePal.where("userName = ?", strAccount).find(UserTable.class);
                if (userTables.size() > 0) {
                    UserTable userTable = userTables.get(0);
                    if (userTable.getPassWord().equals(strPassWord)) {
                        mSharePreferenceUtil.setIsLogin(true);
                        mSharePreferenceUtil.setUserName(strAccount);
                        mSharePreferenceUtil.setUserPassword(strPassWord);
                        openActivity(MainActivity.class);

                        mSharePreferenceUtil.setUserRole(userRole);

                        MyToast.showToast("登录成功");
                        finish();

                    } else {
                        MyToast.showToast("密码错误");
                    }
                } else {
                    MyToast.showToast("此账号不存在");
                }

            }
        });

        getBinding().tvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(RegisterActivity.class);
            }
        });

        getBinding().rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb01) {
                    //管理员
                    userRole = 1;
                } else {
                    userRole = 2;
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();

        getBinding().edAccount.setText(mSharePreferenceUtil.getUserName());
        getBinding().edPassWord.setText(mSharePreferenceUtil.getUserPassword());

        if (mSharePreferenceUtil.getUserRole() == 1) {
            getBinding().rb01.setChecked(true);
        } else {
            getBinding().rb02.setChecked(true);
        }
    }
}